#include <Windows.h>
#include <tchar.h>
#include <atlbase.h>
#include "../base/utils.h"
#include "../base/logging.h"
#include "pcagent.h"

namespace
{
	HINSTANCE dll_instance = NULL;
}

extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void*)
{
	switch (reason)
	{
	case DLL_PROCESS_ATTACH:
		dll_instance = instance;
		break;

	default:
		break;
	}
	return TRUE;
}

extern "C" int APIENTRY DllEntry(TCHAR* cmd_line, int cmd_show)
{
	OPT_LOG(L1, (_T("[DllEntry][%s]"), cmd_line));

	bool is_local_system = false;
	HRESULT hr = IsSystemProcess(&is_local_system);
	assert(SUCCEEDED(hr));
	if (SUCCEEDED(hr))
	{
		LKPCAgent lkpcagent(is_local_system);
		hr = lkpcagent.Main(dll_instance, cmd_line, cmd_show);
		if (FAILED(hr)) 
			OPT_LOG(LEVEL_ERROR, (_T("[Main failed][%s][0x%08x]"), cmd_line, hr));
	}

	OPT_LOG(L1, (_T("[DllEntry exit][0x%08x]"), hr));
	return static_cast<int>(hr);
}